<?php

namespace app\admin\controller;

use app\admin\validate\SysMenu as SysMenuValidate;
use app\common\business\SysMenu as SysMenuBus;
use app\common\lib\toCamelCase;

class SysMenu extends AuthBase
{
    //菜单列表
    public function menuList(){
        $where = [];
        $menuName = $this->request->get('menuName',"");
        $status = $this->request->get("status","");
        if($menuName){
            $where[] = ['menu_name', 'like', '%'.$menuName.'%'];
        }
        if ($status !== null && $status !== '') {
            // 如果$status不为null且不为空字符串，则添加条件到$where数组
            $where[] = ["status", "=", $status];
        }
        try {
            $menuData = (new SysMenuBus())->getNormalMenu($where,"*");
            $menuData = (new toCamelCase())->toCamelCaseArray($menuData);
        }catch (\Exception $e){
            $menuData = [];
        }
        return show(config("status.success"),"菜单列表",$menuData);
    }

    //菜单添加
    public function menuAdd(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验
        $allParams = request()->param();
        $data = (new toCamelCase())->toCamelCaseArray($allParams,'1',2);
        $validate = new SysMenuValidate();
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $result = (new SysMenuBus())->addMenuData($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"新增成功",$result);
    }

    //字典类型修改
    public function menuEdit(){
        $allParams = request()->param();
        $data = (new toCamelCase)->toCamelCaseArray($allParams,1,2);
        try {
            $result = (new SysMenuBus())->updateById($data['menu_id'],$data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"修改成功",$result);
    }

    //菜单详情
    public function menuDetails(){
        $menu = $this->request->param("menu","","trim");
        if(empty($menu)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $menuData = (new SysMenuBus())->getMenuById($menu);
            $menuData = (new toCamelCase())->toCamelCaseArray($menuData);
        }catch (\Exception $e){
            $menuData = [];
        }
        return show(config("status.success"),"菜单详情",$menuData);
    }

    //删除菜单
    public function menuDel(){
        $menu = $this->request->param("menu","","trim");
        if(empty($menu)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $result = (new SysMenuBus())->DelMenuId($menu);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"删除成功",$result);
    }

    //菜单无限极分类
    public function menuTree(){
        $menuData = (new SysMenuBus())->menuTree();
        return show(config("status.success"),"ok",$menuData);
    }
}
